package com.tyl.servlet.bill;

import com.alibaba.fastjson.JSONArray;
import com.tyl.pojo.Bill;
import com.tyl.pojo.Provider;
import com.tyl.pojo.User;
import com.tyl.service.bill.BillService;
import com.tyl.service.bill.BillServiceImpl;
import com.tyl.service.provider.ProviderService;
import com.tyl.service.provider.ProviderServiceImpl;
import com.tyl.utils.Constants;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

public class BillServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method =req.getParameter("method");
        if (method.equals("query")&&method!=null){
            this.query(req,resp);
        }else if (method.equals("view")&&method!=null){
            this.queryView(req,resp,"billview.jsp");
        }else if(method.equals("delbill")&&method!=null){
            this.delBill(req,resp);
        }else if(method.equals("add")&&method!=null){
            this.add(req,resp);
        }else if(method.equals("getproviderlist")&&method!=null){
            this.getProviderList(req,resp);
        }else if(method.equals("modify")&&method!=null){
            this.getBillById(req, resp, "billmodify.jsp");
        }else if (method.equals("modifysave")&&method!=null){
            this.updateBill(req,resp);
        }
    }

    public void query(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String queryProductName = req.getParameter("queryProductName");
        String queryProviderId = req.getParameter("queryProviderId");
        String queryIsPayment = req.getParameter("queryIsPayment");
        List<Provider> providerList = new ArrayList<Provider>();
        ProviderService providerService=new ProviderServiceImpl();
        providerList = providerService.getProviderList("", "");
        req.setAttribute("providerList",providerList);//显示供应商列表
        if (queryProductName == null) {
            queryProductName="";
        }

         List<Bill> billList = new ArrayList<Bill>();
        BillService billService=new BillServiceImpl();
        Bill bill=new Bill();
        //判断是否通过供应商来查询的
        if (queryProviderId==null){
            bill.setProviderId(0);
        }else{
            bill.setProviderId(Integer.parseInt(queryProviderId));
        }

        //判断是否通过是否付款来查询
        if (queryIsPayment==null){
            bill.setIsPayment(0);
        }else{
            bill.setIsPayment(Integer.parseInt(queryIsPayment));
        }
        bill.setProductName(queryProductName);

        try {
            billList = billService.getBillList(bill);//显示订单列表
            req.setAttribute("billList",billList);
            req.setAttribute("queryProductName",queryProductName);
            req.setAttribute("queryProviderId",queryProviderId);
            req.setAttribute("queryIsPayment",queryIsPayment);
            req.getRequestDispatcher("billlist.jsp").forward(req,resp);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    public  void queryView(HttpServletRequest req, HttpServletResponse resp,String url) throws ServletException, IOException {
        String billId=req.getParameter("billid");
        if (billId!=null){
            BillService billService=new BillServiceImpl();
            Bill bill=new Bill();
            try {
                bill = billService.getBillId(billId);
                req.setAttribute("bill",bill);
                req.getRequestDispatcher(url).forward(req, resp);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public void delBill(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String billId=req.getParameter("billid");
        int flag=0;
        HashMap<String, String> map = new HashMap<String, String>();
        if (billId!=null){
            BillService billService=new BillServiceImpl();
            try {
                flag = billService.deleteBill(billId);
                if (flag>0){//删除成功
                    map.put("delResult","true");
                }else if (flag<0){
                    map.put("delResult","false");
                }else{//订单不存在
                    map.put("delResult","notexist");
                }
                resp.setContentType("application/json");
                PrintWriter writer = resp.getWriter();
                writer.write(JSONArray.toJSONString(map));
                writer.flush();
                writer.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public  void add (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String billCode = req.getParameter("billCode");
        String productName = req.getParameter("productName");
        String productDesc = req.getParameter("productDesc");
        String productUnit = req.getParameter("productUnit");

        String productCount = req.getParameter("productCount");
        String totalPrice = req.getParameter("totalPrice");
        String providerId = req.getParameter("providerId");
        String isPayment = req.getParameter("isPayment");

        Bill bill=new Bill();
        bill.setBillCode(billCode);
        bill.setProductName(productName);
        bill.setProductDesc(productDesc);
        bill.setProductUnit(productUnit);
        bill.setProductCount(new BigDecimal(productCount));
        bill.setTotalPrice(new BigDecimal(totalPrice));
        bill.setProviderId(Integer.parseInt(providerId));
        bill.setIsPayment(new Integer(isPayment));
        bill.setCreationDate(new Date());

        bill.setCreatedBy(((User) req.getSession().getAttribute(Constants.USER_SESSION)).getId());
        int flag = 0;
        BillService billService = new BillServiceImpl();
        try {
            flag = billService.addBill(bill);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        System.out.println("add flag -- > " + flag);
        if (flag>0) {
            resp.sendRedirect(req.getContextPath() + "/jsp/bill.do?method=query");
        } else {
            req.getRequestDispatcher("billadd.jsp").forward(req, resp);
        }
    }
    public  void getProviderList(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        List<Provider> providerList = new ArrayList<Provider>();
        ProviderService providerService = new ProviderServiceImpl();
        providerList = providerService.getProviderList("", "");
        //把providerList转换成json对象输出
        resp.setContentType("application/json");
        PrintWriter outPrintWriter = resp.getWriter();
        outPrintWriter.write(JSONArray.toJSONString(providerList));
        outPrintWriter.flush();
        outPrintWriter.close();
    }

    private void getBillById(HttpServletRequest rep, HttpServletResponse resp, String url)
            throws ServletException, IOException{
        String billid = rep.getParameter("billid");
        if (billid!=null) {
            BillService billService = new BillServiceImpl();
            Bill bill = null;
            try {
                bill = billService.getBillId(billid);
                rep.setAttribute("bill", bill);
                rep.getRequestDispatcher(url).forward(rep, resp);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public  void  updateBill(HttpServletRequest rep, HttpServletResponse resp) throws IOException, ServletException {
        String billId = rep.getParameter("id");
        String billCode=rep.getParameter("billCode");
        String productName=rep.getParameter("productName");
        String productUnit=rep.getParameter("productUnit");
        String productCount=rep.getParameter("productCount");
        String totalPrice=rep.getParameter("totalPrice");
        String providerId=rep.getParameter("providerId");
        String isPayment=rep.getParameter("isPayment");
        Bill bill = new Bill();
        bill.setId(Integer.parseInt(billId));
        bill.setBillCode(billCode);
        bill.setProductName(productName);
        bill.setProductUnit(productUnit);
        bill.setProductCount(new BigDecimal(productCount));
        bill.setTotalPrice(new BigDecimal(totalPrice));
        bill.setProviderId(Integer.valueOf(providerId));
        bill.setIsPayment(Integer.valueOf(isPayment));
        bill.setModifyBy(((User) rep.getSession().getAttribute(Constants.USER_SESSION)).getId());
        bill.setModifyDate(new Date());
        BillService billService=new BillServiceImpl();
        int flag=0;
        try {
            flag = billService.updateBill(bill);
            if (flag>0){
                resp.sendRedirect(rep.getContextPath() + "/jsp/bill.do?method=query");
            }else{
                rep.getRequestDispatcher("billmodify.jsp").forward(rep, resp);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
